package com.xjh.mange.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.mange.model.Money;
import com.xjh.mange.model.MoneyDetail;
import com.xjh.mange.service.MoneyDetailService;
import com.xjh.mange.service.MoneyService;
import com.xjh.mange.vo.LayuiTableVo;
import com.xjh.mange.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @auther xjh
 * @create 2021/2/20
 **/
@Controller
@RequestMapping("/moneydetail")
@Slf4j
public class MoneyDetailController {
    @Autowired
    private MoneyDetailService moneyDetailService;
    @Autowired
    private MoneyService moneyService;


    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVo list(int page, int limit, String condition) {
        IPage<MoneyDetail> page1 = new Page<>(page, limit);
        QueryWrapper<MoneyDetail> detailQueryWrapper = new QueryWrapper<>();
        detailQueryWrapper.orderByDesc("create_time");
        moneyDetailService.page(page1, detailQueryWrapper);
        return LayuiTableVo.ok(page1.getRecords(), (int) page1.getTotal());
    }

    @PostMapping("/out")
    @ResponseBody
    public ResponseVO out(MoneyDetail moneyDetail) {
        log.info("支出:[{}]", moneyDetail);
        moneyDetail.setType("支出");
        moneyDetail.setCreateTime(new Date());

        List<Money> moneyList = moneyService.list();
        Money money = moneyList.get(0);
        log.info("原有资产:[{}]", money);
        BigDecimal subtract = money.getMoney().subtract(moneyDetail.getMoney());
        log.info("减少后的资产:[{}]", subtract);
        money.setMoney(subtract.setScale(2));
        moneyService.updateById(money);

        boolean save = moneyDetailService.save(moneyDetail);
        return save ? ResponseVO.ok("成功") : ResponseVO.error("失败");
    }

    @PostMapping("/in")
    @ResponseBody
    public ResponseVO in(MoneyDetail moneyDetail) {
        log.info("收入:[{}]", moneyDetail);
        moneyDetail.setType("收入");
        moneyDetail.setCreateTime(new Date());

        List<Money> moneyList = moneyService.list();
        Money money = moneyList.get(0);
        log.info("原有资产:[{}]", money);
        BigDecimal add = money.getMoney().add(moneyDetail.getMoney());
        log.info("增加后的资产:[{}]", add);
        money.setMoney(add.setScale(2));
        moneyService.updateById(money);

        boolean save = moneyDetailService.save(moneyDetail);
        return save ? ResponseVO.ok("成功") : ResponseVO.error("失败");
    }


    @GetMapping("/show")
    @ResponseBody
    public ResponseVO show() {
        String date = DateUtil.format(new Date(), "yyyy-MM-dd");
        log.info("今日：" + date);
        QueryWrapper<MoneyDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("type", "支出").between("create_time", date, date + " 23:59:59");
        List<MoneyDetail> outs = moneyDetailService.list(wrapper);
        BigDecimal outCount = new BigDecimal("0");
        if (CollUtil.isNotEmpty(outs)) {
            for (MoneyDetail out : outs) {
                outCount = outCount.add(out.getMoney());
            }
        }
        QueryWrapper<MoneyDetail> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("type", "收入").between("create_time", date, date + " 23:59:59");
        List<MoneyDetail> ins = moneyDetailService.list(wrapper2);
        BigDecimal inCount = new BigDecimal("0");
        if (CollUtil.isNotEmpty(ins)) {
            for (MoneyDetail in : ins) {
                inCount = inCount.add(in.getMoney());
            }
        }
        HashMap inout = new HashMap();
        inout.put("in", inCount);
        inout.put("out", "-" + outCount);
        return ResponseVO.ok(inout);
    }

    @DeleteMapping("/deleteOne")
    @ResponseBody
    public ResponseVO delOne(Integer id) {
        log.info("删除明细id:[{}]", id);
        MoneyDetail moneyDetail = moneyDetailService.getById(id);
        if (moneyDetail != null) {
            boolean b = moneyDetailService.removeById(id);
            if (b) {
                if ("支出".equals(moneyDetail.getType())) {
                    List<Money> list = moneyService.list();
                    Money money = list.get(0);
                    money.setMoney(money.getMoney().add(moneyDetail.getMoney()));
                    boolean b1 = moneyService.updateById(money);
                    return b1 ? ResponseVO.ok("删除成功") : ResponseVO.error("删除失败");
                }
                if ("收入".equals(moneyDetail.getType())) {
                    List<Money> list = moneyService.list();
                    Money money = list.get(0);
                    money.setMoney(money.getMoney().subtract(moneyDetail.getMoney()));
                    boolean b1 = moneyService.updateById(money);
                    return b1 ? ResponseVO.ok("删除成功") : ResponseVO.error("删除失败");
                }
            }
            return ResponseVO.error("删除失败");
        } else {
            return ResponseVO.error("不存在");
        }
    }

}
